﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Admin_Components_SaleOrder_SaleOrderTransfer : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadDataForm();
        }
    }
    private void loadDataForm()
    {
        int ShopID = Session["ShopID"] == null ? 0 : int.Parse(Session["ShopID"].ToString());

        Utils.loadEmployeeToCombo(cboEmployeeDeliveryCode, 3, ShopID, 1);

       
        //Load tinh thanh
        int ProvinceID = Session["ProvinceID"] == null ? 0 : int.Parse(Session["ProvinceID"].ToString());
        //load Shop
        //int ShopID = Session["ShopID"] == null ? 0 : int.Parse(Session["ShopID"].ToString());
        int isShopAll = (ShopID == 0 ? 1 : 0);
        Utils.loadShopByProvinceToCombo(cboShop, isShopAll, ProvinceID, ShopID, "");

        Utils.LoadCodeDetail(cboStatus, "SaleOderStatus", 1);
        cboStatus.SelectedValue = "N";
        cboStatus.Enabled = false;
        gv.DataSource = new DataTable();
        gv.DataBind();
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/SaleOrderList", true);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (cboShop.SelectedValue == "0")
        {
            JavascriptUtil.LoadMessage(UpdatePanel1, false, "Vui lòng chọn cửa hàng chuyển đi");
            return;
        }
        LoadData();
        gv.DataBind();
    }

    protected void gvData_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        LoadData();
    }

    private void LoadData()
    {
        DBDataContext db = LINQUtil.GetDBWriteOnly();
        String Status = this.cboStatus.SelectedValue;
        String Shop = this.cboShop.SelectedValue;
        String SaleOrderCode = this.txtSaleOrderCode.Text.Trim();
        String Customer = this.txtCustomerName.Text.Trim();
        String EmployeeDelivery = this.cboEmployeeDeliveryCode.SelectedValue;//0
        String Product = this.txtProduct.Text.Trim();
        String PersonReceipt = this.txtReceiptPerson.Text.Trim();
        String PersonPhone = this.txtReceiptPhoneNumer.Text.Trim();
        String PersonAddress = this.txtReceiptAdress.Text.Trim();
        int ProvinceID = Session["ProvinceID"] == null ? 0 : int.Parse(Session["ProvinceID"].ToString());
        DateTime FromDate = new DateTime(2014, 06, 01);
        if (this.dtpFromDate.SelectedDate != null)
        {
            FromDate = this.dtpFromDate.SelectedDate.Value;
        }
        DateTime ToDate = new DateTime(2099, 12, 31);
        if (this.dtpToDate.SelectedDate != null)
        {
            ToDate = this.dtpToDate.SelectedDate.Value;
        }
        int PageNumber = gv.CurrentPageIndex + 1;
        int PageSize = gv.PageSize;
        var q = db.SaleOrder_Search(SaleOrderCode, Status, ProvinceID, Shop, Customer,EmployeeDelivery, Product, PersonReceipt, PersonPhone, PersonAddress, FromDate, ToDate, PageSize, PageNumber, "", "0").ToList();

        if (q.Count > 0)
        {
            gv.Height = 250;
            gv.ClientSettings.Scrolling.AllowScroll = true;
            gv.ClientSettings.Scrolling.UseStaticHeaders = true;
            gv.VirtualItemCount = q[0].TotalRows.Value;
            gv.DataSource = q;
        }
        else
        {
            gv.DataSource = new DataTable();
        }
    }

    protected void gvData_ItemCommand(object sender, GridCommandEventArgs e)
    {
        string commandName = e.CommandName;

        if (commandName == "TransferItem")
        {

            try
            {
                DBDataContext db = LINQUtil.GetDBWriteOnly();
                GridDataItem item = e.Item as GridDataItem;
                String SaleOrderID = e.CommandArgument.ToString();
                string shopFrom = cboShop.SelectedItem.Text;
                int shopFromID = int.Parse(cboShop.SelectedValue);
                //Button btnTransfer = (Button)sender;
                //GridDataItem dataItem = (GridDataItem)btnTransfer.NamingContainer;
                RadComboBox cbo = (RadComboBox)item.FindControl("cboShopTo");
                string ShopTo = cbo.SelectedValue;
                var so = db.SaleOrders.FirstOrDefault(s => s.ID == int.Parse(SaleOrderID));
                if (so != null)
                {
                    if (shopFromID.ToString() =="0")
                    {
                        JavascriptUtil.LoadMessage(UpdatePanel1, false, "Vui lòng chọn cửa hàng chuyển đi");
                        return;
                    }
                    if (ShopTo.ToString() == "0")
                    {
                        JavascriptUtil.LoadMessage(UpdatePanel1, false, "Vui lòng chọn cửa hàng chuyển đến");
                        return;
                    }

                    if (ShopTo == shopFromID.ToString())
                    {
                        JavascriptUtil.LoadMessage(UpdatePanel1, false, "Cửa hàng chuyển đi và cửa hàng chuyển đến không được trùng nhau");
                        return;
                    }
                    so.ShopID = int.Parse(ShopTo);
                    so.DeliveryEmployeeID = 0;
                    so.LastUpdatedBy = Session["UserName"].ToString();
                    so.LastUpdatedDateTime = DateTime.Now;
                    so.Remark = "Chuyen tu kho " + shopFrom;

                    SaleOrderTransfer sot = new SaleOrderTransfer();
                    sot.SaleOrderID = int.Parse(SaleOrderID);
                    sot.FromShopID = shopFromID;
                    sot.ToShopID = int.Parse(ShopTo);
                    sot.CreatedBy = Session["UserName"].ToString();
                    sot.LastUpdatedBy = Session["UserName"].ToString();
                    sot.CreatedDateTime = DateTime.Now;
                    sot.LastUpdatedDateTime = DateTime.Now;
                    sot.Status = 'N';
                    db.Insert<SaleOrderTransfer>(sot);
                    db.Update<SaleOrder>(so);
                    db.SubmitChanges();
                }
                JavascriptUtil.LoadMessage(UpdatePanel1, true, "Thành công!");
                //LoadData();
                //gv.DataBind();
                gv.Rebind();
            }
            catch (Exception ex)
            {
                JavascriptUtil.LoadMessage(UpdatePanel1, false, "Có lỗi xảy ra!");
                ControlUtil.Log("----------------------------");
                ControlUtil.Log("SaleOrderTransfer-gvData_ItemCommand");
                ControlUtil.Log(ex.ToString());
                ControlUtil.Log("----------------------------");
            }
        }
    }

    protected void gv_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            RadComboBox cboShopTo = (RadComboBox)item.FindControl("cboShopTo");
            Utils.loadShopToCombo(cboShopTo,1, 0,"");
           
        }
    }
}